﻿
Imports std
Imports storoomc
Imports elpmis
Imports runrroom.delegateClient

Public Module rrdelegt
    Public Const defaultConfigfile As String = "rrdelegt.ini"
    Private _jobinfoRootDirectory As String = Nothing
    Private _jobinfoDirectory As String = Nothing
    Private _storoomAddress As IPEndPoint2 = Nothing

    Sub New()
        lpmsstor.register()
        context.insertSharedFunction(New rrdelegtFunctions._updateStatus())
        context.insertSharedVariable("jobid", New const_bare_variable(typeDefination.string, jobid()))
    End Sub

    Private Function __jobinfoRootDirectory() As String
        assert(Not isemptystring(_jobinfoRootDirectory))
        Return _jobinfoRootDirectory
    End Function

    Private Function workingpath() As String
        Static _workingpath As String = Nothing
        If _workingpath Is Nothing Then
            _workingpath = combinepath(filesystem.temppath(), jobid())
        End If
        Return _workingpath
    End Function

    Private Function __jobinfoDirectory() As String
        assert(Not isemptystring(_jobinfoDirectory))
        Return _jobinfoDirectory
    End Function

    Private Function jobinfopath() As String
        Return __jobinfoRootDirectory() + __jobinfoDirectory()
    End Function

    Private Function storoomAddress() As IPEndPoint2
        assert(Not _storoomAddress Is Nothing)
        Return _storoomAddress
    End Function

    Private Function jobstartScript() As String
        Static _jobstartScript As String = Nothing
        If _jobstartScript Is Nothing Then
            _jobstartScript = combinepath(workingpath(), runrroom.defaultValue.jobstartScript)
        End If
        Return _jobstartScript
    End Function

    Private Function download() As Boolean
        storoom.initial(storoomAddress().Address(), storoomAddress().Port())
        createDirectory(workingpath())
        Dim files As vector(Of String) = Nothing
        If Not directorystore.listfiles(jobinfopath(), files) Then
            raiseError(errorHandle.errorType.warning, "cannot get file list under directory " + jobinfopath())
            Return False
        Else
            assert(Not files Is Nothing)
            For i As Int64 = 0 To files.size() - 1
                Dim buff As bytes = Nothing
                If Not directorystore.readfile(jobinfopath(), files(i), buff) Then
                    raiseError(errorHandle.errorType.warning, _
                               "cannot download file from directory ", jobinfopath(), ", filename ", files(i))
                    Return False
                Else
                    Try
                        IO.File.WriteAllBytes(combinepath(workingpath(), files(i)), +buff)
                    Catch ex As Exception
                        raiseError(errorHandle.errorType.exclamation, _
                                   "cannot write to file ", combinepath(workingpath(), files(i)), ", ex ", ex.Message)
                    End Try
                End If
            Next

            Return True
        End If
    End Function

    Private Function startscript() As Boolean
        Dim app As lpmsapp = Nothing
        If lpmsapp.parse(jobstartScript(), app) Then
            Return app.main()
        Else
            raiseError(errorHandle.errorType.user, "cannot parse script ", jobstartScript())
            Return False
        End If
    End Function

    Public Sub main(ByVal args() As String)
        errorHandle.initial(errorHandle.NOLOGFILEMARK)
        counter.initial(counter.NOCOUNTERFILEMARK)

        assert(Not args Is Nothing AndAlso args.Length() > 0)
        Dim configfile As String = Nothing
        If args Is Nothing OrElse args.Length() = 1 Then
            configfile = defaultConfigfile
            _jobinfoDirectory = args(0)
        Else
            configfile = args(0)
            _jobinfoDirectory = args(1)
        End If
        Dim config As configParser = Nothing
        config = New configParser()
        assert(config.open(configfile))
        Dim s As String = Nothing
        s = config.parameter(configNaming.rrdelegt.section, _
                             configNaming.rrdelegt.storoom, _
                             configNaming.defaultValue.rrdelegt.storoom)
        assert(IPEndPoint2.parse(s, _storoomAddress))
        _jobinfoRootDirectory = config.parameter(configNaming.rrdelegt.section, _
                                                 configNaming.rrdelegt.jobInfoKeyPrefix, _
                                                 configNaming.defaultValue.rrdelegt.jobInfoRootDirectory)
        directorystore.normalizeDirectory(_jobinfoRootDirectory)
        assert(download())
        assert(startscript())
    End Sub
End Module

Namespace configNaming
    Namespace rrdelegt
        Public Module _rrdelegt
            Public Const section As String = "rrdelegt"
            Public Const storoom As String = "storoom"
            Public Const jobInfoKeyPrefix As String = "jobInfoKeyPrefix"
        End Module
    End Namespace

    Namespace defaultValue
        Namespace rrdelegt
            Public Module _rrdelegt
                Public Const storoom As String = runrroom.defaultValue.storoom
                Public Const jobInfoRootDirectory As String = runrroom.defaultValue.jobinfoRootDirectory
            End Module
        End Namespace
    End Namespace
End Namespace